package com.briup.sxau.web.Interceptor;

import com.briup.sxau.util.Result;
import com.briup.sxau.util.SxauException;
import io.jsonwebtoken.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@Component
public class SxauInterceptor implements HandlerInterceptor {
    /*
    * 在进入控制器之前拦截
    *
    **/
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //日志记录请求方式和请求路径
        log.info(request.getMethod() + ":" + request.getRequestURI());

//  获取请求头部的token
        String token = request.getHeader("token");
        if (ObjectUtils.isEmpty(token)) {
            throw new SxauException("用户没有登录哇");
        }

        else {
            Jwt jwt = Jwts.parser().setSigningKey("shitongtong").parse(token);
            return true;

           /* try {
            Jwt jwt = Jwts.parser().setSigningKey("shitongtong").parse(token);
                return true;
            } catch (ExpiredJwtException e) {
                throw new SxauException("登录信息已经过期请重新登录");
            } catch (MalformedJwtException e) {
                throw new SxauException("非法token");
            }
       */ }

    }
}
